package com.condroid;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.widget.Toast;

/**
 * This is Shtcutsetting class and it contains different
 * methods to be invoked by other classes related to shortcut 
 * settings.
 * @author Yasir Ali & Jun gao
 * Condroid Summer CSD 2011 - KTH
 */
public class ShtcutSetting {
	
	
	public static String config;
	public static String commandOfIndex;
	public static String[] loadedData;
	private static int count;
	public static SharedPreferences newInstance;
	public static SharedPreferences.Editor shtEditor;
	public static String showText;
	public static String showText1;
	private static boolean commandMade;


	
	
	
	
	
	
	public ShtcutSetting() {
		
		
	}

	
	public static void setConfig(String newConfig){
		config = newConfig;
	}
	
	public static String getConfig(){
		return config;
		
	}
	
	

	
	/**
	 * Checks the syntax the of the string given by the user in shtcut
	 * activity.
	 * This method is invoked by make command method to make sure that
	 * the commands by the user are correctly spelled.
	 * 
	 * @param checkValue
	 * @return boolean
	 */
	public static boolean checkSyntax(String checkValue) {
		
		boolean isCorrect = false;
		int z=0;
		int len = checkValue.length();
			for (z=0;z<+len;z++){
				char c=checkValue.charAt(z);
					if( Character.isUpperCase(c) 
					|checkValue.charAt(z) == '*' 
					|checkValue.charAt(z) == '&'
					|checkValue.charAt(z) == '+' 
					|checkValue.charAt(z) == '-' 
					|checkValue.charAt(z) == '/' 
					|checkValue.charAt(z) == '?' 
					|checkValue.charAt(z) == '#' ){
					isCorrect = false;
					break;
					}else {
					isCorrect = true;
					

				}
				
			}
			
		return isCorrect;
	}

	
	/**
	 * This method can be used by different classes of the application
	 * to get command by giving the parameter index
	 * @param index
	 * @return String
	 */
	
	public static String getCommand(int index){
		
		
		if(commandMade){
		commandOfIndex = loadedData[index];
		return commandOfIndex;
		
		}else return null;
		
		
		
	}
	

	
	
	/**
	 * This method is used to get the shortcut by giving index of the
	 * short cut 
	 * @param index
	 * @return String [] array
	 */
	public static String[] getShortcut(int index){
		showText = newInstance.getString("Shtcutvalues", "Not able to Load");
		String[] gS = makeCommand(showText); // Split shortcut text by ;
		loadedData=gS[index].split(":"); // Split the the short by :
		return loadedData; // return shortcut in an array
	}
	
	
			
	/**
	 * This method is used to add the shortcut by giving String of the
	 * short cut
	 * @param sht
	 * @return int (index)
	 */
	public static int addShortcut(String sht){
		showText = newInstance.getString("Shtcutvalues", "Not able to Load");
		String[] gS = makeCommand(showText); // Split shortcut text by ;
		if(gS!=null){
		int count=0;
		//Check whether the shortcut is already exist or not
		for (String k: gS){
			if(gS[count].equals(sht)){
			return count;
			}
		count++;
		}
		// Adding the shortcut
		showText+=sht;
		shtEditor = newInstance.edit();
		shtEditor.putString("Shtcutvalues", showText);
		shtEditor.commit();
		gS=makeCommand(showText);
		count = gS.length;
		}
		
		return count--;
	}
	
	
	/** */
	
	/**
	 * This method is used to delete the shortcut by giving index of the
	 * short cut
	 * @param indexx
	 * @return boolean
	 */
	public static boolean delShortcut(int indexx){
		showText = newInstance.getString("Shtcutvalues", "Not able to Load");
		String[] dShort = makeCommand(showText); // Split shortcut text by ;
		if (dShort!=null){
			count=0;
		//Check whether the shortcut is already exist or not
	
				dShort[indexx]=null;
				showText="";
				count=0;
				for (String n : dShort){
				if(dShort[count]!=null){
				showText+= (dShort[count]+";");
				}
				count++;
				}
				shtEditor = newInstance.edit();
				shtEditor.putString("Shtcutvalues",showText);
				shtEditor.commit();
				
				return true;
			}
	
		return false;
	}
	
	
	/***/
	
	/**
	 * make commands and check the syntax of each commands
	 * @param shtcutUserText2
	 * @return String [] array
	 */
	public static String[] makeCommand(String shtcutUserText2) {
		
		
		loadedData = shtcutUserText2.split(";"); // Split user text by ";"

		boolean isCorrect = false;
		count=0;
		for (String x: loadedData){ // For loop to check each segment syntax
	     
	            isCorrect = ShtcutSetting.checkSyntax(loadedData[count]); 
	            // checks syntax
	            if (isCorrect != true){
	            	loadedData[count]=null;
	            	return loadedData;
	            }
	            
	        count++;
		}
		return loadedData;
	}
	
	
	/***/
	
	
	/**
	 * Saves the Data into the file
	 * @param shtcutUserText2
	 * @return
	 */
	
	public static boolean saveConfiguration(String shtcutUserText2) {
		if(checkConfig(shtcutUserText2)){
		loadedData = makeCommand(shtcutUserText2);
		count=0;
		for(String f: loadedData){
			if (loadedData[count]==null){
				return false;
			}
		count++;	
		} 
		shtEditor = newInstance.edit(); // make new instance of the file
		shtEditor.putString("Shtcutvalues", shtcutUserText2); // puts the new data into the file
		shtEditor.commit(); // commits new changes
		return true;
		}
		else 
		return false;
				
	}	
	
	/***/
		
	
	
	/**
	 * Give privileges to other classes and activity to use these shared 
	 * preferences (This method needs to be called in order to use any
	 * of the methods in this class).
	 * 
	 * @param context
	 * @return String
	 */
	public static String getConfiguration(Context context) {
		newInstance = PreferenceManager.getDefaultSharedPreferences(context);
		newInstance = context.getSharedPreferences(ShtcutActivity.filename,0);
		showText = newInstance.getString("Shtcutvalues", "Not able to Load");
		return showText;
	}
	
	/***/
	

    /**
	 * Checks whether the statement are correctly formatted or not!!
	 * @param shtcutValue
	 * @return boolean
	 */
			private static boolean checkConfig(String shtcutValue) {
					if (shtcutValue.endsWith(";")){
						return true;
					}
					return false;
					
			}

	

	
}
